module dgl.BoundingBox;

import dgl.BoundingSphere;
import dgl.graphics.Color;
import derelict.opengl.gl;
import dgl.GameTime;
import dgl.Vector3;

struct BoundingBox
{
	public Vector3 min;
	
	public Vector3 max;
	
	public static BoundingBox opCall( Vector3 min, Vector3 max )
	{
		BoundingBox ret;
		ret.min = min;
		ret.max = max;
		return ret;
	}
	
	public bool intersects( BoundingSphere sphere )
	{
		Vector3 vector = Vector3.clamp( sphere.center, this.min, this.max );
		float num = Vector3.distanceSquared( sphere.center, vector );
		return ( num <= ( sphere.radius * sphere.radius ) );
	}
	
	public void draw( GameTime gameTime )
	{
		Vector3 v0 = min;
		Vector3 v1 = Vector3( max.x, min.y, min.z );
		Vector3 v2 = Vector3( min.x, max.y, min.z );
		Vector3 v3 = Vector3( min.x, min.y, max.z );
		Vector3 v4 = Vector3( min.x, max.y, max.z );
		Vector3 v5 = Vector3( max.x, min.y, max.z );
		Vector3 v6 = Vector3( max.x, max.y, min.z );
		Vector3 v7 = max;
		
		glBegin( GL_LINE_STRIP );
		Vector3 color = Color.white.toVector3();
		glColor3fv( color.ptr );
		
		glVertex3fv( v0.ptr );
		
		glVertex3fv( v1.ptr );
		glVertex3fv( v5.ptr );
		glVertex3fv( v3.ptr );

		glVertex3fv( v0.ptr );
		glVertex3fv( v2.ptr );
		glVertex3fv( v4.ptr );
		
		glVertex3fv( v3.ptr );
		glVertex3fv( v5.ptr );
		glVertex3fv( v7.ptr );
		
		glVertex3fv( v4.ptr );
		glVertex3fv( v2.ptr );
		glVertex3fv( v6.ptr );
		
		glVertex3fv( v7.ptr );
		glVertex3fv( v5.ptr );
		glVertex3fv( v1.ptr );

		glVertex3fv( v6.ptr );
		glVertex3fv( v2.ptr );
		glVertex3fv( v0.ptr );
		
		glEnd();
	}
	
	public char[] toString()
	{
		char[] ret = "";
		ret ~= min.toString();
		ret ~= ", ";
		ret ~= max.toString();
		return ret;
	}
}